﻿using System;
using System.IO;
using System.Text;


namespace Project.Tool
{
	public class JsonFile
	{
		/// <summary>
		/// 获取Json文件内容
		/// </summary>
		/// <param name="filePath">文件路径</param>
		/// <returns></returns>
		public static string Get(string filePath) {
			string json = string.Empty;
			using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
				{
					json = sr.ReadToEnd();
				}
			}
			return json;
		}
		/// <summary>
		/// 写入文件
		/// </summary>
		/// <param name="JsonStr">Json字符串</param>
		/// <param name="filePath">保存路径</param>
		/// <returns></returns>
		public static bool Add(string JsonStr,string filePath,string fileName) {

			string
				path = filePath + fileName;

            bool rezult = false;
			if (!Directory.Exists(filePath))
				Directory.CreateDirectory(filePath);
			using (FileStream fs = File.Create(path)) {
				using (StreamWriter sw = new StreamWriter(fs))
				{
					try{
						sw.Write(JsonStr);
						rezult = true;
					}
					catch {
					}
				}
			}
			return rezult;
		}
	}
}
